<!DOCTYPE html>
<html>
    <?php

    include_once "../DB/db.php";
    include_once "../class/CategoryClass.php";
    include_once "../class/UserClass.php";

    if ($user->isLoggedOn() && $user->isAdmin()) { //check if admin
        ?>
        <head>
            <title>Nettbutikk</title>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <link rel="stylesheet" type="text/css" href="CSS/mainFrame.css" />
            <style>
                #name{
                    width: 300px;
                }
                #description{
                    width: 700px;
                    height: 200px;
                }
                #newCatSubmit{
                    width: 130px;
                    height: 50px;
                }
                #delCatName{
                    color: red;
                    font-weight: bold;
                }
                .catExistErr{
                    width: 230px;
                    height: 50px;
                    border-style: double;
                    background-color:khaki;
                    position: absolute;
                    margin-left: 700px;
                    margin-top: 170px;
                    text-align: center;
                }
                .updateConfirm{
                    position: absolute;
                    width: 230px;
                    height: 50px;
                    border-style: dotted;
                    background-color: lightcyan;
                    position: absolute;
                    margin-left: 500px;
                    margin-top: 30px;
                    text-align: center;
                }
            </style>
        </head>
        <body>
            <div class="wrapper">
                <?php
                include_once "../MainFrame/header.php";
                include_once "../MainFrame/menuBar.php";
                include_once "../MainFrame/sideBar.php";
                ?>

                <div class="content">

                    <?php
                    if (isset($_POST['selectAlterCatSubmit']) && $_POST['selectAlterCatSubmit']) { //if edit category
                        $_GET['catId'] = $_POST['catId'];
                        alterCatDisplay();  //display category edit form
                    } else if (isset($_POST['selectDeleteCatSubmit']) && $_POST['selectDeleteCatSubmit']) { // if delete category
                        $_GET['catId'] = $_POST['catId'];
                        deleteCatDisplay(); //display category delete form
                    }
                    ?>
                    <?php
                    if (!isset($_GET['alterCat'])) {    //checks if user gona edit category
                        $_GET['alterCat'] = "undefined"; //if not set undefinded
                    }
                    if ($_GET['alterCat'] == 'newCat' && isset($_GET['alterCat'])) {
                        newCatDisplay(); //if user gona create new category
                    } else if ($_GET['alterCat'] == 'deleteCat' && isset($_GET['alterCat'])) {
                        deleteCatDisplay(); //if user gona delete category
                    } else if ($_GET['alterCat'] == 'alterCat' && isset($_GET['alterCat'])) {
                        alterCatDisplay(); // if user gona edit category
                    }
                    ?>
                </div>

                    <?php
                }
                include_once "../MainFrame/footer.php";
                ?>
        </div>
    </body>


<?php
//function that desplays form for create category
function newCatDisplay() { ?> 
        <h2>Lag ny Kategori</h2>
        <form id="newCategoryForm" name="newCategory" method="POST" action="" enctype="multipart/form-data">
            <label for="categoryName">Name:</label>
            <input id="name" type="text" name="catName" required="required" maxlength="32"><br><br>
            <label for="catDescription">Description:</label><br/>
            <textarea cols="1" rows="30" id="description" name="catDescription" required="required"></textarea>
            <input id="newCatSubmit" type="submit" name="newCatSubmit" value="Submit"/>
        </form>
<?php }
// displays form for delete category
function deleteCatDisplay() {
    ?>
        <?php
        global $cat;
        $catName = $cat->getCatName($_GET['catId']);
        ?>
        <h2>Slett kategori</h2>
        <p>Er du sikkert på at du vil slette kategori: <span id=delCatName><?php echo $catName; ?></span>?</p>
        <form name="delCategory" method="POST" action="">
            <input type='hidden' name='kid' value='<?php echo $_GET["catId"]; ?>'/>
            <input type="submit" id="delCatSubmit" name="delCatSubmit" value="Slett kategorien"/>
        </form>
        <p style="font-style: italic;">Merk: Å slette denne kategorien vil kun føre til at attributten "delete" <br>
            i databasen blir aktivert. Kategorien vil ikkje bli sletta før administrator <br>
            fjernar den frå databasa.</p>
<?php }
// displays form for edit category
function alterCatDisplay() {
    ?>
        <?php
        if (!isset($_GET['updated'])) 
            $_GET['updated'] = "undefined";
        if ($_GET['updated'] != "undefined" && isset($_GET['updated']))
            showUpdateConfirm();// displays a message

        global $cat;
        $catInfo = $cat->getCategoryInfoId($_GET['catId']); // get all info from that category
        $row = $catInfo->fetch();
        ?>
        <h2>Endre kategori</h2>
        <form id="alterCategoryForm" name="alterCategory" method="POST" action="" enctype="multipart/form-data">
            <input type="hidden" name="catId" value="<?php echo $row['kid']; ?>">
            <label for="categoryName">Name:</label>
            <input id="name" type="text" name="catName" maxlength="32" value="<?php echo $row['name']; ?>" required="required"><br><br>
            <label for="catDeleted">Deleted:</label><br>
            <input type="radio" name="catDeleted" value="yes" <?php if ($row['deleted'] == 1) echo 'checked'; ?> /> Yes<br />
            <input type="radio" name="catDeleted" value="no"  <?php if ($row['deleted'] == 0) echo 'checked'; ?> /> No <br/><br/>
            <label for="catDescription">Description:</label><br/>
            <textarea cols="1" rows="30" id="description" name="catDescription"required="required"><?php echo $row['description']; ?></textarea>
            <input id="newCatSubmit" type="submit" name="alterCatSubmit" value="Bekreft endring"/>
        </form>
        <?php           //displays the form for edit a category
    }

    //------------------------- warning messages -----------------------------
    function showCatExistError() {
        ?>
        <div class="catExistErr">
            <p>Error: This category already exists.</p>
        </div>
    <?php
}

function showUpdateConfirm() {
    ?>
        <div class="updateConfirm">
            <p>Kategorien er oppdatert.</p>
        </div>
    <?php
}

// *********************** New Category ***************************
if (isset($_POST['newCatSubmit']) && $_POST['newCatSubmit']
        && ($cat->categoryExists($_POST['catName']) === false)) {
    $cat->insertNewCategory($_POST['catName'], $_POST['catDescription']);
    $catInfo = $cat->getCategoryInfo($_POST['catName']);
    $catId = $catInfo->fetch();
    $catId = $catId['kid'];
    header("Location: adminItem.php?alterItem=newItem&catId=$catId");
} else if (isset($_POST['newCatSubmit']) && $cat->categoryExists($_POST['catName']) === true) {
    showCatExistError();
}

// ****************************Delete Category *****************************
if (isset($_POST['delCatSubmit']) && $_POST['delCatSubmit']) {
    $cat->deleteCategory($_POST['kid']);
    header("Location: /adminViews/myAdminSite.php");
}

// ************************* Alter Category ***************************
if (isset($_POST['alterCatSubmit']) && $_POST['alterCatSubmit']) {
    $catName = $_POST['catName'];
    $catDeleted = $_POST['catDeleted'];
    $catDescription = $_POST['catDescription'];
    $catId = $_POST['catId'];
    if ($catDeleted == 'no')
        $catDeleted = 0;
    else
        $catDeleted = 1;
    $category_query = "
            UPDATE itemcategory
            SET name='$catName', deleted=$catDeleted, description='$catDescription'
            WHERE kid=$catId";
    $categoryUpdate = $db->prepare($category_query);
    $categoryUpdate->execute();
    header("Location: adminCategory.php?alterCat=alterCat&catId=$catId&updated=updated");
}
?>
</html>